/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package logging.report;

import java.util.Collection;
import java.util.logging.Level;
import org.apache.commons.pool2.impl.GenericObjectPool;
import pgentity.User;
import share.StringBuilderPoolFactory;
import share.TimeUtil;

/**
 *
 * @author Salm
 */
class RecordImpl implements Record {
    protected static final char DELIMITER = '\t';
    
    private static final GenericObjectPool<StringBuilder> SB_POOL =
            StringBuilderPoolFactory.makePool();
    
    private final PGLogCategory category;
    private final String time;
    private final long lTime;
    
    private String cachedLog;

    public RecordImpl(PGLogCategory category, long time) {
        this.category = category;
        this.lTime = time;
        this.time = TimeUtil.viDateFormat(time);
    }

    @Override
    public String getCategory() {
        return this.category.toString();
    }
    
    @Override
    public long getTime() {
        return lTime;
    }

    protected int logFields()
    {
        return 1;
    }
    
    protected int logSize()
    {
        return time.length();
    }
    
    protected void buildLog(StringBuilder builder)
    {
        logField(builder, time);
    }

    @Override
    public String toString() {
        if (this.cachedLog == null)
        {
            StringBuilder sBuilder = null;
            try {
                sBuilder = SB_POOL.borrowObject();
                int len = this.logSize() + this.logFields();
                sBuilder.ensureCapacity(len);
                
                buildLog(sBuilder);
                
                this.cachedLog = sBuilder.toString();
            } catch (Exception ex) {
                java.util.logging.Logger.getLogger(PGRecord.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally
            {
                if (sBuilder != null)
                {
                    SB_POOL.returnObject(sBuilder);
                }
            }
        }
        
        return cachedLog;
    }
    
    protected static void logField(StringBuilder builder, String field)
    {
        builder.append(field);
        builder.append(DELIMITER);
    }
    
    protected static void logField(StringBuilder builder, Object field)
    {
        logField(builder, field.toString());
    }
    
    protected static String[] toStrings(Collection<?> objs, int fix)
    {
        String[] s = new String[fix];
        int i = 0;
        for (Object dat : objs) {
            s[i++] = dat.toString();
            if (i >= fix)
                break;
        }
        
        while (i < fix)
        {
            s[i++] = "";
        }
        
        return s;
    }
    
    protected int len(String[] s)
    {
        int len = 0;
        for (String _s : s) {
            len += _s.length();
        }
        
        return len;
    }
}
